import { config } from '../config'
import { IHouseItem } from '@/types/house'
import { ILoginResponse } from '@/types/common'
import { GetQueryString } from '@/utils'

let areas = ['青秀区', '兴宁区', '江南区', '良庆区', '邕宁区', '西乡塘区', '武鸣区', '横州市', '隆安县', '马山县', '上林县', '宾阳县']
let houseType = ['毛坯房', '精装房', '简装房']

// 添加中文姓名生成配置
const familyNames = ['李', '王', '张', '刘', '陈', '杨', '黄', '赵', '周', '吴', '徐', '孙', '朱', '马', '胡', '郭', '林', '何', '高', '梁', '欧阳', '上官', '司马', '东方', '令狐']
const givenNames = ['伟', '芳', '娜', '丽', '强', '军', '杰', '敏', '静', '波', '婷', '磊', '洋', '宇', '鑫', '慧', '倩', '瑞', '嘉', '晨', '雨', '欣', '宁', '昊']

/**
 * 获取房源列表
 */
export const getHouseListMock = (page: number, type: number) => {
  let wh = [[240, 180], [240, 180]]
  let t = type % 2
  let w = wh[t][0]
  let h = wh[t][1]

  return new Promise((resolve) => {
    const data = Array.from({ length: 15 }, (_, i) => ({
      id: `${page * i + 1}`,
      type: type,
      title: (() => {
        const base = `${areas[i % 3]}·${80 + i}㎡·${houseType[i % 3]}`;
        const leaseKeywords = ['精装公寓', '地铁直达', '独立卫浴', '拎包入住', '短租优选'];
        const newHouseKeywords = ['热销楼盘', '学区房源', '央企开发', '公园地产', '科技住宅'];
        const resaleKeywords = ['满五唯一', '南北通透', '业主急售', '赠送车位', '带看方便'];

        switch (type) {
          case 0: return `${leaseKeywords[i % 3]}·${base}`;
          case 1: return `${newHouseKeywords[i % 3]}·${base}`;
          case 2: return `${resaleKeywords[i % 3]}·${base}`;
          default: return base;
        }
      })(),
      price: 50 + i * 10,
      unit: '万',
      avgPrice: `${10000 + i * 100}/㎡`,
      area: 80 + i,
      roomType: `${2 + Math.floor(i / 3)}室1厅`,
      city: '南宁市',
      district: areas[i % 3],
      cover: `https://picsum.photos/${w}/${h}?random=${page * i + 1}_${type}`,
      tags: ['近地铁', '精装修', '独立卫浴'].slice(0, i % 3 + 1),
      facility: ['空调', '洗衣机', '冰箱', '宽带'].slice(0, i % 4 + 1),
      direction: ['东', '南', '西', '北'][i % 4],
      floor: `${i + 1}/${10 + i}层`,
      totalFloor: 10 + i,
      decoration: houseType[i % 3],
      createdAt: '2024-03-15',
      desc: (() => {
        let random = Math.random();
        if (random < 0.3) {
          return '';
        }
        const newlyKeywords = [`近${i + 1}天内${10 * i + 10}人浏览`, `${i + 1}天内${10 * i + 10}人收藏`, `近${i + 1}天内${10 * i + 10}人关注`, `近${i + 1}天内${10 * i + 10}人点赞`, `近${i + 1}天内${10 * i + 10}人评论`];
        const resaleKeywords = [`入围${areas[i % 3]}热门推荐榜第${i + 1}名`, `近${i + 1}天内${10 * i + 10}人购买`, `近${i + 1}天内${10 * i + 10}人看房`, `近${i + 1}天内${10 * i + 10}人咨询`, `近${i + 1}天内${10 * i + 10}人评价`];

        switch (type % 2) {
          case 0: return `${newlyKeywords[i % 3]}`;
          default: return `${resaleKeywords[i % 3]}`;
        }
      })(),
    })) as IHouseItem[];

    setTimeout(() => {
      resolve({ code: 200, data: data })
    }, Math.random() * 2000)
  })
}

/**
 * 登录
 */
export const loginMock = (body: { username: string; password: string }): ILoginResponse => {
  // 原实现保持不变，添加类型注释
  if (body.password === '123456' && /^1[3-9]\d{9}$/.test(body.username)) {
    return {
      code: 200,
      data: {
        id: Math.floor(Math.random() * 1000),
        role: body.username.startsWith('138') ? 'landlord' : 'tenant',
        username: body.username,
        avatar: 'https://example.com/avatar.png',
        token: 'mock_token_' + Date.now()
      }
    }
  }
  return {
    code: 401,
    message: '密码错误或账号不存在'
  }
}

/**
 * 获取消息列表
 */
export const getMessageListMock = (page: number) => {
  const data = Array.from({ length: 10 }, (_, i) => ({
    id: page * 10 + i,
    name: (() => {
      const family = familyNames[i % 3]
      const givenLength = [1, 2][i % 2]
      const given = Array.from({ length: givenLength }, () => givenNames[Math.floor(Math.random() * givenNames.length)]).join('')
      return family + given
    })(),
    time: new Date(Date.now() - Math.random() * 60000).toISOString(),
    lastMessage: [
      `您好，${['请问什么时候方便看房？', '租金还能优惠吗？', '房子还在吗？'][i % 3]}`,
      `${['合同条款', '物业费', '押金比例'][i % 3]}需要再确认下`,
      `已支付定金，${['请发送合同', '何时签合同？', '需要维修下水道'][i % 3]}`,
      `${['转租咨询', '合租问题', '续租事宜'][i % 3]}需要沟通`
    ][i % 4],
    avatar: `https://picsum.photos/40/40?random=${page * 10 + i}_msg`,
    unreadCount: Math.floor(Math.random() * 5)
  }))
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({ code: 200, data: data })
    }, Math.random() * 2000)
  })
}
